package org.github.core.base;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import java.util.List;
import java.util.Objects;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Lists.newArrayList;

@EqualsAndHashCode(callSuper = true)
@Data
public class PageBo extends Sort {
  /** 当前页 */
  @Min(value = 1)
  private int pageNum = 1;

  /** 页大小 */
  @Max(value = 100)
  @Min(value = 10)
  private int pageSize = 10;

  /** 搜索总数 */
  private boolean searchCount = true;

  /** 多重排序 */
  @NotNull
  private List<Sort> sortList = newArrayList();

  @Override
  public String toString() {
    return super.toString();
  }

  @Override
  public void trim() {
    super.trim();
    sortList.forEach(Sort::trim);
  }

  public <T> MbpPage<T> page() {
    return new MbpPage<>(this);
  }

  @NotNull
  protected List<OrderItem> sortList() {
    return sortList.stream().map(Sort::sort).filter(Objects::nonNull).collect(toImmutableList());
  }
}
